<template>
  <div>
    <el-form :inline="true" :model="page" class="demo-form-inline">
      <el-form-item label="用户名">
        <el-input v-model="page.userName" clearable placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="用户昵称">
        <el-input v-model="page.nickName" clearable placeholder="用户昵称"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="page.phonenumber" clearable placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="findPage()">查询</el-button>
        <el-button type="primary" @click="onAdd">新建</el-button>
      </el-form-item>
    </el-form>

    <el-table
        :data="this.pageData.records"
        border
        style="width: 100%">
      <el-table-column
          prop="id"
          label="id"
          width="80">
      </el-table-column>
      <el-table-column
          prop="userName"
          label="用户名"
          width="100">
      </el-table-column>
      <el-table-column
          prop="nickName"
          label="用户昵称"
          width="100">
      </el-table-column>
      <el-table-column
          prop="phonenumber"
          label="手机号"
          width="150">
      </el-table-column>
      <el-table-column
          prop="email"
          label="用户邮箱"
          width="150">
      </el-table-column>
      <el-table-column
          prop="sex"
          label="性别"
          width="50">
      </el-table-column>
      <el-table-column
          prop="createTime"
          label="创建时间">
      </el-table-column>
      <el-table-column
          prop="updateTime"
          label="修改时间">
      </el-table-column>
      <el-table-column
          prop="remark"
          label="备注">
      </el-table-column>

      <el-table-column
          fixed="right"
          label="操作"
          width="200">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="编辑" placement="top">
            <el-button type="primary" icon="el-icon-edit" @click="updateClick(scope.row)" circle></el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="top">
            <el-button type="danger" icon="el-icon-delete" @click="deleteClick(scope.row.id)" circle></el-button>
          </el-tooltip>
        </template>
      </el-table-column>

    </el-table>

    <!--  分页  -->
    <div style="float: right">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="this.pageData.current"
          :page-size="this.pageData.size"
          layout="total, prev, pager, next"
          :total="this.pageData.total">
      </el-pagination>
    </div>


    <el-dialog
        title="新增"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose">

      <el-form :inline="true" label-position="right" label-width="80px" :model="sysUser" :rules="rules" ref="sysUser"
               class="demo-form-inline">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="sysUser.userName" style="width: 200px" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickName">
          <el-input v-model="sysUser.nickName" style="width: 200px" placeholder="用户昵称"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="sysUser.password" style="width: 200px" placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phonenumber">
          <el-input v-model="sysUser.phonenumber" style="width: 200px" placeholder="手机号"></el-input>
        </el-form-item>
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="sysUser.email" style="width: 200px" placeholder="用户邮箱"></el-input>
        </el-form-item>

        <el-form-item label="性别" prop="sex">
          <el-select v-model="sysUser.sex" style="width: 200px" placeholder="请选择性别">
            <el-option
                v-for="sex in sexs"
                :key="sex.value"
                :label="sex.label"
                :value="sex.value">
            </el-option>
          </el-select>
        </el-form-item>

          <el-form-item label="角色" prop="sysRoleId">
            <el-select v-model="sysUser.sysRoleId" style="width: 200px" placeholder="请选择角色">
              <el-option
                  v-for="sysRole in sysRoles"
                  :key="sysRole.id"
                  :label="sysRole.roleName"
                  :value="sysRole.id">
              </el-option>
            </el-select>

        </el-form-item>

        <div class="container">
          <el-button @click="closeDia()">取 消</el-button>
          <el-button type="primary" @click="onSubmit('sysUser')">提交</el-button>
        </div>

      </el-form>

    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "sysUser",
  data() {
    return {
      sexs: [{
        label: '男',
        value: 0
      },
        {
          label: '女',
          value: 1
        },
      ],
      sysRoles:[],
      dialogVisible: false,
      pageData: {
        records:[],
        total:0,
        size:0,
        current:0
      },
      page: {
        pageNum: 1,
        pageSize: 10,
        userName: '',
        nickName: '',
        phonenumber: '',
        email: '',
      },
      sysUser: {
        id: null,
        userName: '',
        nickName: '',
        phonenumber: '',
        sex: '',
        password: '',
        email: '',
        sysRoleId: 0,

      },
      rules: {
        userName: [
          {required: true, message: '请输入角色名称', trigger: 'blur'},
        ],
        nickName: [
          {required: true, message: '请输入用户昵称', trigger: 'blur'},
        ],
        phonenumber: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        sex: [
          {required: true, message: '请选择性别', trigger: 'change'},
        ],
        sysRoleId: [
          {required: true, message: '请选择角色', trigger: 'change'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
      }
    }
  },
  methods: {
    deleteClick: function (id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete("/SysUser/delete?id=" + id).then((res) => {
          if (res.data.code === '200') {
            this.$message({
              message: res.data.msg,
              type: 'success'
            });
            this.findPage();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });


    },
    updateClick: function (sysUser) {
      this.sysUser = sysUser
      this.dialogVisible = true;
    },
    handleClose: function () {
      this.dialogVisible = false;
    },
    onSubmit: function (sysUser) {
      this.$refs[sysUser].validate((valid) => {
        if (valid) {
          var url = '';
          if (this.sysUser.id === null) {
            url = '/SysUser/insert';
          } else {
            url = '/SysUser/update';
          }

          this.$http.post(url, this.sysUser).then((res) => {
            if (res.data.code === '200') {
              this.dialogVisible = false;
              this.$message({
                message: res.data.msg,
                type: 'success'
              });
              this.findPage();
            }
          })
        } else {
          return false;
        }
      })

    },
    closeDia: function () {
      this.dialogVisible = false;
      this.sysUser = {
        id: null,
        userName: '',
        nickName: '',
        phonenumber: '',
        sex: '',
        password: ''
      }
    },
    onAdd: function () {
      this.sysUser = {
        id: null,
        userName: '',
        nickName: '',
        phonenumber: '',
        sex: '',
        password: ''
      }
      this.dialogVisible = true;
    },
    findPage: function () {
      this.$http.get('/SysUser/listByPage?pageNum=' + this.page.pageNum + '&pageSize=' + this.page.pageSize + '&userName=' + this.page.userName).then((res) => {
        this.pageData = res.data.data
      })
    },
    handleSizeChange(val) {
      console.log(`页: ${val}`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page.pageNum = val;
      this.findPage();
    },
    findRole:function () {
      this.$http.get('/SysRole/list').then((res) => {
        this.sysRoles = res.data
      })
    }
  },
  mounted() {
    this.findRole();
    this.findPage();
  }
}
</script>

<style lang="less">
.container {
  margin-top: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
